﻿using System;
using System.Windows.Forms;
using Polar.Monitor;

namespace PolarSyncApp
{
    public partial class WatchForm : Form
    {
        private readonly WristUnitListener _listener = new WristUnitListener();
        private IWristUnitConnection _connection;
        public delegate void WriteText(string value);
        public WriteText InvokeWrite;

        public WatchForm()
        {
            InitializeComponent();
            InvokeWrite = new WriteText(Write);
        }

        void WristUnitMountedEventHandler(object sender, WristUnitConnectionEventArgs e)
        {
            _connection = e.Connection;
            Write("Connected to '{0}'", _connection.Info.Name);

            _connection.Disconnected += WristUnitUnmountedEventHandler;

            logoPictureBox.Image = _connection.ReadLogo();

            foreach (var head in _connection.ReadHeaders())
            {
                var exercise = e.Connection.GetExercise(head);
                Write("-- {0}",
                      exercise.Timestamp.ToString("MM/dd/yyyy HH:MM"));
                Write("   calories: {0}", exercise.Calories);
                Write("   duration: {0} hours and {1} minutes.",
                      exercise.Duration.Hours,
                      exercise.Duration.Minutes);
                Write("   fat burn: {0}%", exercise.FatConsumption);
                Write("   heart rate: {0}(avg) {1}(max) {2}(min)", exercise.Heartrate.Average,
                      exercise.Heartrate.Maximum,
                      exercise.Heartrate.Minimum);
                Write("   Zones: ");
                foreach (var z in exercise.Zones)
                {
                    Write("      -Zone {1}: {0}", z.Inside.ToString(),
                        z.GetType().Name
                        );
                    
                }
            }

            Write("Features:");
            if (_connection.Info.HasFeature(Feature.ACTIVITY))
            {
                Write("Activity");
                _connection.ReadActivityRecords();
            }
            if (_connection.Info.HasFeature(Feature.CALENDAR))
            {
                Write("Calendar");
                _connection.ReadCalendarItems();
            }
            if (_connection.Info.HasFeature(Feature.CARDIO))
            {
                Write("Cardio");
                _connection.ReadCardioProgram();
            }
            if (_connection.Info.HasFeature(Feature.CLOCK))
            {
                Write("Clock");
                _connection.ReadDualTime();
                _connection.ReadTimeOfDay();
            }
            if(_connection.Info.HasFeature(Feature.OWNINDEX))
            {
                Write("OwnIndex");
                _connection.ReadOwnIndices();
            }
            if(_connection.Info.HasFeature(Feature.OWNRELAX))
            {
                Write("OwnRelax");
                _connection.ReadOwnRelaxes();
            }
            if (_connection.Info.HasFeature(Feature.USER))
            {
                Write("User");
                _connection.ReadTimeStampedUser();
                _connection.ReadUser();
            }
            if (_connection.Info.HasFeature(Feature.SUMMARY))
            {
                Write("Summary");
                _connection.ReadWeekSummaries();
            }
        }

        void WristUnitUnmountedEventHandler(object sender, WristUnitConnectionEventArgs e)
        {
            _connection = e.Connection;
            Write("Disconnected from '{0}'", e.Connection.Info.Name);
        }

        private void Write(string value)
        {
            if(InvokeRequired)
            {
                OutputTextBox.Invoke(InvokeWrite, value);
            }
            else
            {
                OutputTextBox.AppendText(value + "\r\n");    
            }
        }

        private void Write(string value, params object[] args)
        {
            Write(string.Format(value, args));
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _listener.WristUnitMounted += WristUnitMountedEventHandler;
            _listener.Start();
        }
    }
}

